load("@rules_cc//cc:defs.bzl", "cc_library")
load("//bazel:flags.bzl", "CPP_FLAGS", "MAPLIBRE_FLAGS")

cc_library(
    name = "default-collator",
    srcs = [
        "src/mbgl/i18n/collator.cpp",
    ],
    copts = CPP_FLAGS + MAPLIBRE_FLAGS,
    deps = [
        "//:mbgl-core",
        "//vendor:nunicode",
    ],
)

cc_library(
    name = "render-test-bin",
    srcs = [
        "src/mbgl/render-test/main.cpp",
    ],
    visibility = [
        "//render-test:__pkg__",
    ],
    deps = [
        "//render-test:render-test-lib",
    ],
)

cc_library(
    name = "utf_conversion",
    srcs = ["src/mbgl/util/utf.cpp"],
    copts = CPP_FLAGS + MAPLIBRE_FLAGS + [
        "-Wno-deprecated-declarations",
    ],
    deps = [
        "//:mbgl-core",
    ],
)

# From: platform/ios/core-files.json
# Mostly platform/default
cc_library(
    name = "mbgl-default",
    srcs = [
        "src/mbgl/gfx/headless_backend.cpp",
        "src/mbgl/gfx/headless_frontend.cpp",
        "src/mbgl/map/map_snapshotter.cpp",
        "src/mbgl/platform/time.cpp",
        "src/mbgl/storage/asset_file_source.cpp",
        "src/mbgl/storage/database_file_source.cpp",
        "src/mbgl/storage/file_source_manager.cpp",
        "src/mbgl/storage/file_source_request.cpp",
        "src/mbgl/storage/local_file_request.cpp",
        "src/mbgl/storage/local_file_source.cpp",
        "src/mbgl/storage/main_resource_loader.cpp",
        "src/mbgl/storage/mbtiles_file_source.cpp",
        "src/mbgl/storage/offline.cpp",
        "src/mbgl/storage/offline_database.cpp",
        "src/mbgl/storage/offline_download.cpp",
        "src/mbgl/storage/online_file_source.cpp",
        "src/mbgl/storage/pmtiles_file_source.cpp",
        "src/mbgl/storage/sqlite3.cpp",
        "src/mbgl/text/bidi.cpp",
        "src/mbgl/util/compression.cpp",
        "src/mbgl/util/filesystem.cpp",
        "src/mbgl/util/monotonic_timer.cpp",
        "src/mbgl/util/png_writer.cpp",
        "src/mbgl/util/thread_local.cpp",
    ] + select({
        "//:metal_renderer": ["src/mbgl/mtl/headless_backend.cpp"],
        "//conditions:default": ["src/mbgl/gl/headless_backend.cpp"],
    }) + select({
        "@platforms//os:linux": [
            "src/mbgl/i18n/number_format.cpp",
            "src/mbgl/layermanager/layer_manager.cpp",
            "src/mbgl/storage/http_file_source.cpp",
            "src/mbgl/text/local_glyph_rasterizer.cpp",
            "src/mbgl/util/async_task.cpp",
            "src/mbgl/util/image.cpp",
            "src/mbgl/util/jpeg_reader.cpp",
            "src/mbgl/util/logging_stderr.cpp",
            "src/mbgl/util/png_reader.cpp",
            "src/mbgl/util/run_loop.cpp",
            "src/mbgl/util/string_stdlib.cpp",
            "src/mbgl/util/thread.cpp",
            "src/mbgl/util/timer.cpp",
            "src/mbgl/util/webp_reader.cpp",
        ],
        "@platforms//os:osx": [
            "src/mbgl/util/async_task.cpp",
            "src/mbgl/util/run_loop.cpp",
            "src/mbgl/util/timer.cpp",
        ],
        "//conditions:default": [],
    }),
    hdrs = [
        "include/mbgl/gfx/headless_backend.hpp",
        "include/mbgl/gfx/headless_frontend.hpp",
        "include/mbgl/map/map_snapshotter.hpp",
        "include/mbgl/storage/file_source_request.hpp",
        "include/mbgl/storage/local_file_request.hpp",
        "include/mbgl/storage/merge_sideloaded.hpp",
        "include/mbgl/storage/offline_database.hpp",
        "include/mbgl/storage/offline_download.hpp",
        "include/mbgl/storage/offline_schema.hpp",
        "include/mbgl/storage/sqlite3.hpp",
        "include/mbgl/text/unaccent.hpp",
    ] + select({
        "//:metal_renderer": ["include/mbgl/mtl/headless_backend.hpp"],
        "//conditions:default": ["include/mbgl/gl/headless_backend.hpp"],
    }),
    copts = CPP_FLAGS + MAPLIBRE_FLAGS,
    includes = [
        "include",
    ],
    linkopts = select({
        "@platforms//os:macos": [
            "-lsqlite3",
            "-luv",
            "-lz",
            "-lcurl",
            "-lpng",
            "-ljpeg",
            "-lwebp",
            "-licuuc ",
            "-licui18n",
            "-licudata",
        ],
        "//conditions:default": [],
    }),
    visibility = [
        "//platform:__pkg__",
        "//platform/darwin:__pkg__",
        "//platform/ios:__pkg__",
        "//platform/linux:__pkg__",
        "//platform/macos:__pkg__",
        "//test:__pkg__",
    ],
    deps = [
        ":utf_conversion",
        "//:mbgl-core",
    ] + select({
        "@platforms//os:ios": [
            "//platform/darwin:darwin-loop",
        ],
        "@platforms//os:linux": [
            "default-collator",
            "//vendor:sqlite",
        ],
        "@platforms//os:osx": [
            ":default-collator",
            "//platform/darwin:darwin-loop",
            "@libuv",
        ],
    }),
)
